package com.bf.zk.node;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.bf.zk.constants.Constants;
import com.bf.zk.node.Node;
import com.bf.zk.user.User;
import com.bf.tool.ConfirmInfo;

/**
 * 刻录机节点修改管理界面
 * 
 * @author Administrator
 *
 */
public class NodeModify extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JDialog nodeManagerModify;
	@SuppressWarnings("unused")
	private boolean flag = false;
	private JTextField nodeName;
	private JTextField ipAddr;
	@SuppressWarnings("unused")
	private JFrame parent;

	public NodeModify(JFrame parent, boolean modal) {
		this.parent = parent;
		// 显示设置
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		nodeManagerModify = new JDialog(this, "节点修改", true);
		nodeManagerModify.setSize(400, 240);
		nodeManagerModify.setDefaultCloseOperation(2);
		// nodeManager.setLocation(200, 200);
		// 居中显示
		nodeManagerModify.setLayout(null);
		nodeManagerModify.setLocationRelativeTo(parent);

		// 节点名称
		JLabel label1 = new JLabel("节点名称：");
		label1.setBounds(50, 10, 80, 30);
		//
		nodeName = new JTextField(Constants.Modify0);
		nodeName.setEnabled(false);
		nodeName.setBounds(130, 10, 150, 30);

		// IP地址
		JLabel label2 = new JLabel("IP地址：");
		label2.setBounds(50, 70, 80, 30);
		//
		ipAddr = new JTextField(Constants.Modify1);
		ipAddr.setBounds(130, 70, 150, 30);

		// 节点增加确定
		JButton nodeModify1 = new JButton("确定");
		nodeModify1.setActionCommand("nodeModify1");
		nodeModify1.setBounds(100, 140, 70, 30);

		// 节点增加取消
		JButton nodeModifyCancel = new JButton("取消");
		nodeModifyCancel.setActionCommand("nodeModifyCancel");
		nodeModifyCancel.setBounds(200, 140, 70, 30);

		nodeManagerModify.add(label1);
		nodeManagerModify.add(label2);
		nodeManagerModify.add(nodeName);
		nodeManagerModify.add(ipAddr);
		nodeManagerModify.add(nodeModify1);
		nodeManagerModify.add(nodeModifyCancel);

		nodeModify1.addActionListener(this);
		nodeModifyCancel.addActionListener(this);
		nodeManagerModify.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// 节点修改确定
		if (e.getActionCommand().equals("nodeModify1")) {
			int rs = -1;
			Connection conn = null;
			String nodeNameStr = nodeName.getText();
			String nodeIpStr = ipAddr.getText();
			// (ConfirmInfo.ConfirmUser(nodeNameStr))&&
			if ((ConfirmInfo.ConfirmIP(nodeIpStr))) {
				try {
					String fileName = "./sqlite/znzk.db";
					Class.forName("org.sqlite.JDBC");
					conn = DriverManager.getConnection("jdbc:sqlite:" + fileName);
					Statement stmt = conn.createStatement();
					//System.out.println(Constants.Modify0);
					rs = stmt.executeUpdate("UPDATE znzk_node set nodename='" + nodeNameStr + "',nodeip='" + nodeIpStr
							+ "' WHERE nodename='" + Constants.Modify0 + "'");
					Constants.addNode = new Node();
					Constants.addNode.setName(nodeNameStr);
					Constants.addNode.setIPAdd(nodeIpStr);
					conn.close();
				} catch (Exception e2) {
				}
				if (1 == rs) {
					Constants.nodeOrNot = true;
					nodeManagerModify.setVisible(false);
				}
				// db.closed();
				// db.closed();
			} else if (!(ConfirmInfo.ConfirmIP(nodeIpStr))) {
				// ipAddr.setText("");
				JOptionPane.showMessageDialog(nodeManagerModify, "IP不符合规则!", "警告", JOptionPane.WARNING_MESSAGE);

			} else {
				// nodeName.setText("");
				// ipAddr.setText("");
				JOptionPane.showMessageDialog(nodeManagerModify, "节点、IP不符合规则!", "警告", JOptionPane.WARNING_MESSAGE);
			}
		}

		// 节点修改取消
		if (e.getActionCommand().equals("nodeModifyCancel")) {
			Constants.Modify0 = "";
			Constants.Modify1 = "";
			Constants.Modify2 = "";

			// parent.setVisible(true);
			Constants.nodeOrNotCan = true;
			nodeManagerModify.setVisible(false);
		}

	}
}
